﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ServiceModel;
using System.Reflection;

namespace DEMO.BusinessLogicLayer
{
    public class WCF_Util<T>
    {
        private WCF_Util()
        { }

        public static object CallService(string ServiceAddress,string MethodName,object[] parameters, BasicHttpBinding oBasicHttpBinding = null)
        {
            
            try
            {
                if(oBasicHttpBinding==null)
                {
                    oBasicHttpBinding=new BasicHttpBinding();
                }

                ChannelFactory<T> oChannelFactory = new ChannelFactory<T>(oBasicHttpBinding, new EndpointAddress(ServiceAddress));
                T oProxy = oChannelFactory.CreateChannel();
                Type oType = oProxy.GetType();
                MethodInfo oMethodInfo = oType.GetMethod(MethodName);
                object result = oMethodInfo.Invoke(oProxy, BindingFlags.Public | BindingFlags.InvokeMethod, null, parameters, System.Globalization.CultureInfo.CurrentCulture);
                return result;
            }
            catch (Exception ex)
            {
                
                throw ex;
            }
        }
        public static object CallService(string ServiceAddress, string MethodName, object[] parameters, WSHttpBinding oWSHttpBinding = null)
        {

            try
            {
                if (oWSHttpBinding == null)
                {
                    oWSHttpBinding = new WSHttpBinding();
                }

                ChannelFactory<T> oChannelFactory = new ChannelFactory<T>(oWSHttpBinding, new EndpointAddress(ServiceAddress));
                T oProxy = oChannelFactory.CreateChannel();
                Type oType = oProxy.GetType();
                MethodInfo oMethodInfo = oType.GetMethod(MethodName);
                object result = oMethodInfo.Invoke(oProxy, BindingFlags.Public | BindingFlags.InvokeMethod, null, parameters, System.Globalization.CultureInfo.CurrentCulture);
                return result;
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
    }
}